jQuery.noConflict();
$ = jQuery;
$(document).ready(function() {
    $(".editBtn").live('click', function() {
        var tableParent = $(this).parent().parent();
        // determine which type to edit
        var c = (tableParent.attr('id').split(":"))[1].split("_")[0];
        
        //sets the id field
        var val = tableParent.find("input[id$='tblPid']").val();
        $("input[id$='pkey']").val(val);
        
        if (c == "teachers")
            teacherEdit(tableParent);
        else if (c == "courses")
            courseEdit(tableParent);
        $("button[id$='subBtn']").text("Save");
        
    });
});

var teacherEdit = function(tableParent) {
    setInput("user", tableParent);
    setInput("pass", tableParent);
    setInput("name", tableParent);
    setInput("email", tableParent);
}

var courseEdit = function(tableParent) {
    setInput("name", tableParent);
    setInput("year", tableParent);
    var t = tableParent.find("span[id$='teacher']").text();
    $("[id$='formteacher'] option:contains('" + t + "')").attr('selected', 'selected');
}

// Set a input with value from the corresponding element
//name = id of element to append, parent = parent of the base element
var setInput = function(name, parent) {
    var val = parent.find("span[id$='" + name + "']").text();
    $("[id$='form" + name + "']").val(val);
}
